﻿#region
/* ===============================================
 * 作者：guopeng           时间：2012/2/4 11:48:34
 * 
 * 
 * 类描述：招募
 * 
 * 
 * =============================================== */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jobs.Contact;
using Jobs.Inf.DataContext;
using Jobs.Inf.Protector;

namespace Jobs.Domain.Sheet
{
    public class Recruit : Inf.Entity.RecruitSheet
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="agent">代理人用户名</param>
        /// <param name="AdvertiserId">商家Id</param>
        public Recruit(string agent, int advertiserId)
        {
            this.Recruiter = agent;
            this.AdvertiserId = advertiserId;
        }

        /// <summary>
        /// 发布一个新的招募信息
        /// <param name="type"></param>
        /// <param name="subject"></param>
        /// <param name="expire">有效时间（单位：天），如：30.则表示30天后过期</param>
        /// <param name="position"></param>
        /// </summary>
        public bool NewRecruit(int type,int subject,int expire,List<PositionMoudle> position)
        {
            if (position == null || !position.Any())
            {
                throw new ContextException("请设置招募的职位", ExceptionCode.ParameterInvalid);
            }
            using (Context ctx = new Context())
            {
                var recruit = ctx.RecruitSheet.Add(new Inf.Entity.RecruitSheet()
                {
                    AdvertiserId = this.AdvertiserId,
                    Expire = DateTime.Now.AddDays(expire),
                    PublishDate = DateTime.Now,
                    Recruiter = this.Recruiter,
                    RecruitSubject = subject,
                    RecruitType = type
                });
                if (ctx.SaveChanges() > 0)
                {
                    if (position != null)
                    {
                        foreach (var item in position)
                        {
                            ctx.Position.Add(new Inf.Entity.Position()
                            {
                                Contact = item.Contact,
                                Department = item.Department,
                                Descriptions = item.Descriptions,
                                IsVerify = false,
                                MaxSalary = item.MaxSalary,
                                MinSalary = item.MinSalary,
                                OfficeSpace = item.OfficeSpace,
                                OperatorName = string.Empty,
                                Period = item.Period,
                                RecruitId = recruit.Id,
                                RecruitTitle = item.RecruitTitle,
                                Requirement = item.Requirement,
                                ScanTime = 0,
                                Trusteeship = false,
                                Vacancy = item.Vacancy
                            });
                        }
                        try
                        {
                            ctx.SaveChanges();
                            return true;
                        }
                        catch (Exception e)
                        {
                            ctx.Delete<Inf.Entity.RecruitSheet>()
                                .WhereSet(c => c.Id, WhereOperator.Equal, recruit.Id)
                                .End()
                                .Excute(); //职位添加出错。删除原记录
                            throw new RuntimeException("添加招募信息时出错", e);
                        }
                    }
                }
            }
            return false;
        }
        /// <summary>
        /// 为招募信息添加新职位信息
        /// </summary>
        /// <param name="recruitId"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        public bool NewPosition(int recruitId, List<PositionMoudle> position)
        {
            if(position == null && !position.Any())
            { if (position == null || !position.Any())
            {
                throw new ContextException("请设置招募的职位", ExceptionCode.ParameterInvalid);
            }}
            using (Context ctx = new Context())
            {
                foreach (var item in position)
                {
                    ctx.Position.Add(new Inf.Entity.Position()
                    {
                        Contact = item.Contact,
                        Department = item.Department,
                        Descriptions = item.Descriptions,
                        IsVerify = false,
                        MaxSalary = item.MaxSalary,
                        MinSalary = item.MinSalary,
                        OfficeSpace = item.OfficeSpace,
                        OperatorName = string.Empty,
                        Period = item.Period,
                        RecruitId = recruitId,
                        RecruitTitle = item.RecruitTitle,
                        Requirement = item.Requirement,
                        ScanTime = 0,
                        Trusteeship = false,
                        Vacancy = item.Vacancy
                    });
                }
                try
                {
                    ctx.SaveChanges();
                    return true;
                }
                catch (Exception e)
                {
                    throw new RuntimeException("添加招募信息时出错", e);
                }
            }
        }
    }
}
